package com.atXiao.video.controller;
import com.atXiao.video.common.entity.ResBody;
import com.atXiao.video.common.rspEnum.BaseRespEnum;
import com.atXiao.video.entity.req.common.PageReq;
import com.atXiao.video.entity.req.video.VideoDownloadReq;
import com.atXiao.video.entity.req.video.VideoUploadReq;
import com.atXiao.video.entity.resp.video.VideoDownloadResp;
import com.atXiao.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
 * 视频处理Controller
 *
 * @author lixiaoxiao
 * @since 2022-10-28
 */
@RestController
@RequestMapping("/video")
public class VideoController {

    @Autowired
    VideoService videoService;


    /**
     * 视频上传
     *
     * @param videoUploadReq 视频上传参数
     * @return
     */
    @PostMapping("/upload")
    public ResBody videoUpload(VideoUploadReq videoUploadReq) {
        ResBody resBody = new ResBody();
        long start = System.currentTimeMillis();
        //参数校验
        MultipartFile videoFile = videoUploadReq.getVideoFile();
        if (videoFile.isEmpty()) {
            //快速返回
            return ResBody.error(BaseRespEnum.EMPTY_FILE.getCode(), BaseRespEnum.EMPTY_FILE.getMsg());
        }

        try {
            videoService.uploadVideo(videoUploadReq);
        } catch (Exception exception) {
            exception.printStackTrace();
            return ResBody.error();
        }
        long end = System.currentTimeMillis();
        //TODO: 统计日志
        return resBody;
    }


    /**
     * 视频下载（查询单条记录）
     *
     * @param videoDownloadReq
     * @return
     */
    @PostMapping("/download")
    public ResBody videoDownload(@RequestBody VideoDownloadReq videoDownloadReq) {
        ResBody resBody = new ResBody();
        long start = System.currentTimeMillis();
        try {
            VideoDownloadResp downloadResp = videoService.downloadVideo(videoDownloadReq);
            resBody.setData(downloadResp);
        } catch (Exception exception) {
            exception.printStackTrace();
            return ResBody.error();
        }
        long end = System.currentTimeMillis();
        //TODO: 统计日志
        return resBody;
    }


    /**
     * 查询视频列表信息
     */
    @PostMapping("/listVideo")
    public ResBody listVideo(@RequestBody PageReq pageReq) {
        ResBody resBody = new ResBody();
        long start = System.currentTimeMillis();
        try {
            List<VideoDownloadResp> videoList = videoService.listVideoInfo(pageReq);
            resBody.setData(videoList);
        } catch (Exception exception) {
            exception.printStackTrace();
            return ResBody.error();
        }
        long end = System.currentTimeMillis();
        //TODO: 统计日志
        return resBody;
    }


}



